package com.tj.jinzao.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.business.session.constant.Extras;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.tj.jinzao.R;
import com.tj.jinzao.activity.ChatActivity;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.FriendBean;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by LiuYX on 2019/4/19.
 */

public class FriendAdapter extends BaseListAdapter<FriendBean, FriendAdapter.ViewHolder> {
    private int type;
    private ArrayList<String> list=new ArrayList<>();
    private ArrayList<String> listPhoto=new ArrayList<>();
    public FriendAdapter(Context context, CopyOnWriteArrayList<FriendBean> data, int type) {
        super(context, data);
        this.type=type;
    }
    public ArrayList<String> getList(){
        return list;
    }
    public ArrayList<String> getListPhoto(){
        return listPhoto;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_friend_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final FriendBean friendBean = mData.get(position);
        if(friendBean!=null&&friendBean.getFriend()!=null){
            if(friendBean.getFriend().getNickname()!=null){
                holder.name.setText(friendBean.getFriend().getNickname());
            }else {
                holder.name.setText("");
            }
            if(type!=2&&type!=3){
                holder.content_lay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(friendBean.getFriend().getId()==null){
                            ToastUtils.showToast(mContext,"数据格式错误");
                            return;
                        }
                        Intent intent = new Intent();
                        intent.putExtra(Extras.EXTRA_ACCOUNT, friendBean.getFriend().getYunxinAccount());
                        intent.putExtra(Extras.EXTRA_CUSTOMIZATION, NimUIKitImpl.commonP2PSessionCustomization);
                        intent.setClass(mContext, ChatActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        mContext.startActivity(intent);
                    }
                });
            }
            if(friendBean.getFriend()!=null&&friendBean.getFriend().getImgurl()!=null){
                Glide.with(mContext).load(Constant.IMAGE_PREFIX + friendBean.getFriend().getImgurl()).into(holder.avatar);
            }else {
                holder.avatar.setImageResource(R.mipmap.default_head_man);
            }
        }else {
            holder.name.setText("");
            holder.avatar.setImageResource(R.mipmap.default_head_man);
        }
        if(type==2){
            holder.check.setVisibility(View.VISIBLE);
            holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if(b){
                        list.add(friendBean.getFriend().getId());
                        listPhoto.add(friendBean.getFriend().getImgurl());
                    }else {
                        list.remove(friendBean.getFriend().getId());
                        listPhoto.remove(friendBean.getFriend().getImgurl());
                    }
                }
            });
        }else {
            holder.check.setVisibility(View.GONE);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, vip, follow, date, content;
        RoundedImageView avatar;
        CheckBox check;
        LinearLayout content_lay;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.nickName);
            avatar = itemView.findViewById(R.id.avatar);
            content_lay = itemView.findViewById(R.id.content_lay);
            check = itemView.findViewById(R.id.check);
        }
    }

}
